import { HomePage } from '../home/home';
import { Component, ChangeDetectorRef } from "@angular/core";
import {
    NavController,
    AlertController,
    NavParams,
    ActionSheetController,
    LoadingController,
    ToastController
} from "ionic-angular";
import { Storage } from "@ionic/storage";
import { HttpService } from "../../providers/http.service";
import { CommonFunc } from "../../providers/commonFunc";
import { appConfig } from '../../providers/config';

@Component({
    templateUrl: "user-meatasis.html",
    providers: [HttpService, CommonFunc]
})
export class MeatasisPage {
    appConfig = appConfig
    userId: any;
    partyType: any;
    userName: any;
    partyName: any;
    partyId: any;
    toId: any;
    toName: any;
    partyList = [];
    holdList = [];
    searchQuery: string = '';
    toParty: any
    constructor(
        public navCtrl: NavController,
        private storage: Storage,
        private httpService: HttpService,
        private alert: AlertController,
        private commonFunc: CommonFunc,
        private actionSheet: ActionSheetController,
        public cd: ChangeDetectorRef,
        private navParams: NavParams,
        private loading: LoadingController,
        private toast: ToastController,
    ) {
        this.storage.get('user').then(val => {

            // if (val.roleName != '电梯安全管理员' && val.roleName != '电梯维保人员') {
            //     const alert1 = this.alert.create({
            //         // title: '',
            //         subTitle: "目前只允许电梯安全管理员和电梯维保人员转移",
            //         buttons: ["确定"]
            //     });
            //     alert1.present().then(() => {
            //         this.navCtrl.setRoot(HomePage);
            //     });
            //     return;
            // }

            this.userId = val.id;
            this.userName = val.userName;
            this.partyType = val.partyType;
            this.partyName = val.partyName;
            this.partyId = val.partyId;

            this.httpService.queryByIdAndTypeList({ partyType: this.partyType }).subscribe(data => {
                if (data.status == 200) {
                    this.partyList = data.data;
                    this.holdList = data.data;
                }
            });
        });

    }

    getItems(ev: any) {
        // Reset items back to all of the items
        this.toParty = null;
        this.partyList = this.holdList;

        // set val to the value of the searchbar
        const val = ev.target.value;

        // if the value is an empty string don't filter the items
        if (val && val.trim() != '') {
            this.partyList = this.partyList.filter((item) => {
                return (item.partyName.toLowerCase().indexOf(val.toLowerCase()) > -1);
            })
        }
    }

    doSubmit() {
        let load = this.loading.create({
            spinner: appConfig.spinner,
            duration: appConfig.timeout
        });
        if (!this.toParty) {
            let tt = this.toast.create({
                message: "请选择你要转移到单位",
                duration: 3000,
                position: "top"
            });
            tt.present();
            return;
        }
        let param = {
            state: '1',
            userId: this.userId,
            userName: this.userName,
            fromId: this.partyId,
            fromName: this.partyName,
            toId: this.toParty.split('|')[0],
            toName: this.toParty.split('|')[1]
        }
        load.present();
        this.httpService.addMeatasis(param).subscribe(data => {
            load.dismiss();
            if (data.status == 200) {
                let tt = this.toast.create({
                    message: "申请成功",
                    duration: 2000,
                    position: "top"
                });
                tt.present().then(() => {
                    this.navCtrl.setRoot(HomePage);
                });
            } else {
                let tt = this.toast.create({
                    message: data.message,
                    duration: 2000,
                    position: "top"
                });
                tt.present();
            }
        })
    }
}

